﻿using System;
using Game2DFramework;
using MechGame.Game.PlayerRobot;
using Microsoft.Xna.Framework;

namespace MechGame.Game.Overlay
{
    class Hud : GameObject
    {
        private const int ReserveSize = 100;

        private readonly Player _player;
        private int _maxReserveCount;
        private int _currentReserveCount;
        private int _currentLifeBar;

        public Hud(Game2D game) : base(game)
        {
            _player = Game.GetGlobalObject<Player>("Player");
        }

        public void Update()
        {
            _maxReserveCount = Math.Max(0, _player.MaxLife / ReserveSize - 1);

            var currentLife = _player.Life;
            while (currentLife > ReserveSize)
            {
                _currentReserveCount++;
                currentLife -= ReserveSize;
            }
            _currentLifeBar = currentLife;
        }

        public void Draw()
        {
            Game.ShapeRenderer.DrawFilledRectangle(8, 8, _currentLifeBar * 2, 8, Color.Red);
            Game.ShapeRenderer.DrawRectangle(8, 8, 200, 8, Color.White);

            for (var i = 0; i < _maxReserveCount; i++)
            {
                if (i < _currentReserveCount) Game.ShapeRenderer.DrawFilledRectangle(8 + i * 10, 20, 8, 8, Color.Yellow);
                Game.ShapeRenderer.DrawRectangle(8 + i * 10, 20, 8, 8, Color.Yellow);
            }
        }
    }
}
